/*
 * @(#)JMXTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.jmx;

import java.util.Map;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.nhncorp.usf.core.Constants;
import com.nhncorp.usf.core.util.JMXUtil;
import junit.framework.Assert;

/**
 * @author Web Platform Development Team
 */
public class JMXTestCase {
    static JVMMBean jvmMBean;
    static WebModuleMBean webModuleMBean;

    @BeforeClass
    public static void initialize() throws Exception {
        jvmMBean = new JVM("main", JVMMBean.class);
        JMXUtil.registerMBean(jvmMBean);
        webModuleMBean = new WebModule("DefaultContext", WebModuleMBean.class);
        JMXUtil.registerMBean(webModuleMBean);
    }

    @AfterClass
    public static void destory() throws Exception {
        if (jvmMBean != null) {
            JMXUtil.unregisterMBean(jvmMBean);
            jvmMBean = null;
        }
        if (webModuleMBean != null) {
            JMXUtil.unregisterMBean(webModuleMBean);
            webModuleMBean.clear();
            webModuleMBean = null;
        }
    }

    @Test
    @SuppressWarnings({"unused", "unchecked"})
    public void testJVMMBean() {
		Map jvmMap = jvmMBean.stat();
        String name = jvmMBean.getName();
        Assert.assertEquals("main", name);
    }

    @SuppressWarnings("unchecked")
	@Test
    public void testWebMBean() {
        String name = webModuleMBean.getName();
        Assert.assertEquals("DefaultContext", name);
        webModuleMBean.setReqCount("/");
        webModuleMBean.setReqCount("/index.jsp");
        webModuleMBean.setReqCount("/");
        webModuleMBean.setReqTime("/", 40000);
        webModuleMBean.setReqTime("/", 60000);
        webModuleMBean.setReqTime("/index.jsp", 50000);
        webModuleMBean.putResultCodeCount("/", Constants.USF_INTERNAL_ERROR_CODE_101);
        webModuleMBean.putResultCodeCount("/index.jsp", Constants.USF_INTERNAL_ERROR_CODE_101);
        webModuleMBean.putResultCodeCount("/index.jsp", Constants.USF_INTERNAL_ERROR_CODE_103);
        webModuleMBean.putResultCodeCount("/index.jsp", Constants.USF_INTERNAL_ERROR_CODE_101);
        Map webModuleMap = webModuleMBean.stat();
        Assert.assertEquals("2", webModuleMap.get("USF#ReqCount#/#A"));
        Assert.assertEquals("1", webModuleMap.get("USF#ReqCount#/index.jsp#A"));
        Assert.assertEquals("1", webModuleMap.get("USF#E_USF_101#/#A"));
        Assert.assertEquals("1", webModuleMap.get("USF#E_USF_103#/index.jsp#A"));
        Assert.assertEquals("2", webModuleMap.get("USF#E_USF_101#/index.jsp#A"));
        Assert.assertEquals("100000", webModuleMap.get("USF#ReqTime#/#A"));
        Assert.assertEquals("50000", webModuleMap.get("USF#ReqTime#/index.jsp#A"));
    }
}